<template>
  <div class="monthStatementBox">
    <div class="monthStatementTop">
      <div class="title">
        <span>工资报表</span>
      </div>
    </div>
    <div v-loading="loading" class="monthStatementTable">
      <div class="itemDropDown">
        <div class="topLab">
          <div />
          <div class="rightLabBox">
            <a
              class="el-button fr el-button--primary el-button--mini"
              title="导出"
              @click="handleExport"
            >导出</a>
          </div>
        </div>
        <el-table id="item" :data="contentData" border style="width: 100%;text-align: center">
          <el-table-column type="index" label="序号" center width="50" />
          <el-table-column prop="username" label="姓名" width="150px" />
          <el-table-column prop="mobile" label="手机号" width="150px" />
          <el-table-column prop="workNumber" label="工号" width="150px" />
          <el-table-column prop="departmentName" label="部门名称" width="150px" />
          <el-table-column prop="inServiceStatus" :formatter="inService" label="在职状态" width="150px" />
          <el-table-column prop="providentFundIndividual" label="公积金个人" width="150px" />
          <el-table-column prop="socialSecurityIndividual" label="社保个人" width="150px" />
          <el-table-column prop="socialSecurityEnterprise" label="社保企业" width="150px" />
          <el-table-column prop="providentFundEnterprises" label="公积金企业" width="150px" />
          <el-table-column prop="socialSecurityProvidentFundEnterprises" label="公积金社保企业" width="150px" />
          <el-table-column prop="currentBaseSalary" label="基本工资" width="150px" />
          <el-table-column prop="currentBaseSalary" label="岗位工资" width="150px" />
          <el-table-column prop="salaryChangeAmount" label="福利津贴" width="150px" />
          <el-table-column prop="attendanceDeductionMonthly" label="考勤扣款" width="150px" />
          <el-table-column prop="currentSalaryTotalBase" label="税前工资合计" width="150px" />
          <el-table-column prop="salaryByTax" label="应纳税工资" width="150px" />
          <el-table-column prop="tax" label="应扣税" width="150px" />
          <el-table-column prop="payment" label="实发工资" width="150px" />
        </el-table>
      </div>
    </div>
    <el-row type="flex" justify="center" align="middle" style="height:60px;">
      <el-col :span="12">
        <el-button size="small" type="primary" @click="pigeonhole">归档{{ yearMonth.substr(4) + '月' }}报表</el-button>
        <el-button size="small" type="primary" @click="newReport">新建报表</el-button>
        <el-button size="small" @click="$router.back()">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getArchivingCont, newReport } from '@/api/salarys'
export default {
  data() {
    return {
      yearMonth: this.$route.params.yearMonth,
      contentData: [],
      loading: false
    }
  },
  created() {
    this.getArchivingCont()
    this.getNextMonth()
  },
  methods: {
    inService(data) {
      return data.inServiceStatus === '1' ? '在职' : '离职'
    },
    async getArchivingCont() {
      try {
        this.loading = true
        this.contentData = await getArchivingCont({ yearMonth: this.yearMonth, opType: 1 })
        this.loading = false
      } catch (error) {
        this.loading = false
      }
    },
    // 归档
    async pigeonhole() {
      try {
        await this.$confirm('您确认归档当月报表吗？(报表归档将覆盖上一次归档记录，无法恢复)', '归档' + this.yearMonth + '报表')
        this.$message.success('归档成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 新建报表
    async newReport() {
      try {
        // 获取到新建报表的年月
        const res = this.getNextMonth()
        // 提示 是否要新建
        await this.$confirm(`你将创建${res.substr(0, 4)}年${res.substr(4)}月的报表`, '新建报表')
        this.yearMonth = res
        // 发请求
        await newReport({ yearMonth: this.yearMonth })
        // 页面重新加载数据
        this.getArchivingCont()
        // 提示操作成功
        this.$message.success('新建报表成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取新建报表年月的函数
    getNextMonth() {
      const year = this.yearMonth.substring(0, 4)
      const month = this.yearMonth.slice(4)
      let year2 = year
      let month2 = parseInt(month) + 1
      if (month2 === 13) {
        year2 = parseInt(year2) + 1
        month2 = 1
      }
      if (month2 < 10) {
        month2 = '0' + month2
      }
      const t2 = year2 + month2
      return t2
    },
    // 导出
    async handleExport() {
      // 这里是按钮被点击, 可以就在这里单独引入需要的库
      const { export_json_to_excel } = await import('@/vendor/Export2Excel')
      //   console.log(export_json_to_excel)
      const dict = {
        '姓名': 'username',
        '手机号': 'mobile',
        '工号': 'workNumber',
        '部门名称': 'departmentName',
        '在职状态': 'inServiceStatus',
        '公积金个人': 'providentFundIndividual',
        '社保个人': 'socialSecurityIndividual',
        '社保企业': 'socialSecurityEnterprise',
        '公积金企业': 'providentFundEnterprises',
        '公积金社保企业': 'socialSecurityProvidentFundEnterprises',
        '基本工资': 'currentBaseSalary',
        '岗位工资': 'currentBaseSalary',
        '福利津贴': 'salaryChangeAmount',
        '考勤扣款': 'attendanceDeductionMonthly',
        '税前工资合计': 'currentSalaryTotalBase',
        '应纳税工资': 'salaryByTax',
        '应扣税': 'tax',
        '实发工资': 'payment'
      }
      const userData = this.contentData.map(item => {
        return this.object2Arrayy(item, dict)
      })
      export_json_to_excel({
        header: Object.keys(dict),
        data: userData
      })
    },
    // 翻译函数
    object2Arrayy(item, dict) {
      const arr = []
      for (const key in dict) {
        const enKey = dict[key]
        const value = item[enKey]
        if (enKey === 'inServiceStatus') {
          const str = value === '1' ? '在职' : '离职'
          arr.push(str)
        } else {
          arr.push(value)
        }
      }

      return arr
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "./../../styles/variables.scss";

.monthStatementBox {
  padding: 20px;
  .monthStatementTop {
    position: relative;
    background: #fff;
    padding: 10px 15px 0 15px;
    border-bottom: solid 1px #f4f4f4;
    .title {
      color: $blue;
      line-height: 40px;
      border-bottom: solid 2px $blue;
      font-size: 18px;
      font-weight: bold;
      display: inline-block;
      padding: 0 25px;
      .yearChange {
        position: absolute;
        top: 5px;
        right: 10px;
      }
    }
  }
  .monthStatementTable {
    background: #fff;
    .itemTopLab {
      border-top: solid 1px #f0f0f0;
      border-bottom: solid 3px #ccc;
      padding: 15px;
      div {
        display: inline-block;
        padding: 0 50px;
        border-right: solid 1px #ccc;
      }
      div:last-child,
      div:first-child {
        border: none;
      }
      .lab {
        position: relative;
        top: -15px;
        padding-right: 0;
        padding-left: 15px;
      }
      .labTit {
        cursor: pointer;
      }
      .title {
        font-size: 16px;
        margin: 10px 0;
        span {
          position: relative;
          bottom: -2px;
          font-size: 13px;
          color: #999;
          margin-left: 5px;
        }
      }
      .itemTit {
        color: #999;
        margin: 8px 0;
        font-size: 13px;
      }
      .itemNum {
        font-size: 20px;
        margin: 0;
      }
    }
    .itemDropDown {
      background: #fff;
      .topLab {
        position: relative;
        padding: 15px;
        div {
          display: inline-block;
          margin: 0 10px;
          span {
            display: inline-block;
            position: relative;
            top: 2px;
            margin-right: 5px;
            width: 15px;
            height: 15px;
            background: $cl-1;
          }
        }
        .rightLabBox {
          position: absolute;
          right: -10px;
          top: 10px;
          div {
            border: solid 1px $green;
            color: $green;
            border-radius: 3px;
            padding: 4px 10px;
            font-size: 14px;
          }
        }
      }
      .act {
        border-bottom: solid 3px $panGreen;
        .lab {
          color: $panGreen;
        }
        .labTit {
          color: $panGreen;
        }
      }
    }
    .itemes:hover {
      background: #fafbff;
    }
    .itemes .lab:hover {
      cursor: pointer;
    }
  }
  .butList {
    border-top: solid 1px #f4f4f4;
    text-align: center;
    background: #fff;
    span {
      display: inline-block;
      background: $green;
      color: #fff;
      padding: 8px 20px;
      border-radius: 3px;
      margin: 10px;
      cursor: pointer;
    }
    .cancel {
      background: #ccc;
      color: #666;
    }
  }
}
</style>
